﻿using System;
using NUnit.Framework;
using System.IO;
using System.Security.Cryptography.Xml.Fakes;
using Microsoft.QualityTools.Testing.Fakes;
using OneLoginPlugin;

namespace Test.HP.HPTRIM.ServiceAPI.SamplePlugins
{
    [TestFixture]
    public class UnitTest1
    {
        private void doFakes()
        {
            ShimSignedXml.AllInstances.CheckSignatureX509Certificate2Boolean = (xmlDoc, cert, verifyOnly) =>
            {
                return true;
            };
            ShimSignedXml.AllInstances.LoadXmlXmlElement = (xmlDoc, el) =>
            {

            };
        }

        [Test]
        public void Test_SessionNotOnOrAfter_NotValid()
        {
            using (ShimsContext.Create())
            {
                doFakes();

                Response response = new Response(new AccountSettings(new ServiceStack.Configuration.AppSettings()));

                response.LoadXml(File.ReadAllText("../../XML/notvalid_sessionnotonorafter.xml"));

                Assert.IsFalse(response.IsValid());
            }

        }


        [Test]
        public void Test_SessionNotOnOrAfter_Valid()
        {
            using (ShimsContext.Create())
            {
                doFakes();

                Response response = new Response(new AccountSettings(new ServiceStack.Configuration.AppSettings()));

                response.LoadXml(File.ReadAllText("../../XML/valid_sessionnotonorafter.xml"));

                Assert.IsTrue(response.IsValid());
            }
        }

        [Test]
        public void Test_Recipient_NotValid()
        {
            using (ShimsContext.Create())
            {
                doFakes();

                Response response = new Response(new AccountSettings(new ServiceStack.Configuration.AppSettings()));

                response.LoadXml(File.ReadAllText("../../XML/notvalid_recipient.xml"));

                Assert.IsFalse(response.IsValid());
            }

        }


        [Test]
        public void Test_Recipient_Valid()
        {
            using (ShimsContext.Create())
            {
                doFakes();

                Response response = new Response(new AccountSettings(new ServiceStack.Configuration.AppSettings()));

                response.LoadXml(File.ReadAllText("../../XML/valid_recipient.xml"));

                Assert.IsTrue(response.IsValid());
            }
        }

        [Test]
        public void Test_InResponseTo_NotValid()
        {
            using (ShimsContext.Create())
            {
                doFakes();

                Response response = new Response(new AccountSettings(new ServiceStack.Configuration.AppSettings()));

                response.LoadXml(File.ReadAllText("../../XML/notvalid_InResponseTo.xml"));

                Assert.IsFalse(response.IsValid());
            }

        }


        [Test]
        public void Test_InResponseTo_Valid()
        {
            using (ShimsContext.Create())
            {
                doFakes();

                AuthRequest.IDs.Add("123");

                Response response = new Response(new AccountSettings(new ServiceStack.Configuration.AppSettings()));

                response.LoadXml(File.ReadAllText("../../XML/valid_InResponseTo.xml"));

                Assert.IsTrue(response.IsValid());
            }
        }


        [Test]
        public void Test_NoAttributes_Valid()
        {
            using (ShimsContext.Create())
            {
                doFakes();

                Response response = new Response(new AccountSettings(new ServiceStack.Configuration.AppSettings()));

                response.LoadXml(File.ReadAllText("../../XML/valid_nothing.xml"));

                Assert.IsTrue(response.IsValid());
            }
        }

        
    }
}
